package com.lx.huimailf.fragment.order;


import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lx.huimailf.R;
import com.lx.huimailf.adapter.PubAdapter;
import com.lx.huimailf.configs.Config;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import okhttp3.Call;

/**
 * A simple {@link Fragment} subclass.
 */
public class OrderDetialFragment extends Fragment {
    private TextView orderno,name,phone,address,peisongStatus,pickupway;
    private TextView payMethod,psTime,getTime,allMoney,realMoney,createTime;
    private RecyclerView recyclerView;
    private String orderNo="";
    private String custid="";
    private String status="0";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_order_detial, container, false);
        initViews(view);
        SharedPreferences sp=getContext().getSharedPreferences("user", Context.MODE_PRIVATE);
        custid=sp.getString("custid","-11111");
        orderNo=getActivity().getIntent().getStringExtra("orderno");
        status=getActivity().getIntent().getStringExtra("status");

        if(status.equals("-1")){
            peisongStatus.setText("未分配配送员");
        }else if(status.equals("0")){
            peisongStatus.setText("等待配送");
        }else if(status.equals("1")){
            peisongStatus.setText("配送中");
        }else if(status.equals("2")){
            peisongStatus.setText("已完成");
        }

        getOrderInfoByOrderno();
        return view;
    }

    private void initViews(View view){
        pickupway=(TextView)view.findViewById(R.id.pickupway);
        recyclerView=(RecyclerView)view.findViewById(R.id.recyclerview);
        orderno=(TextView)view.findViewById(R.id.orderno);
        name=(TextView)view.findViewById(R.id.xingming);
        phone=(TextView)view.findViewById(R.id.phone);
        address=(TextView)view.findViewById(R.id.tv3);
        orderno=(TextView)view.findViewById(R.id.orderno);
        peisongStatus=(TextView)view.findViewById(R.id.tv_status);

        payMethod=(TextView)view.findViewById(R.id.ps_method);
        psTime=(TextView)view.findViewById(R.id.ps_time);
        getTime=(TextView)view.findViewById(R.id.get_time);
        allMoney=(TextView)view.findViewById(R.id.allprice);
        realMoney=(TextView)view.findViewById(R.id.realmoney);
        createTime=(TextView)view.findViewById(R.id.createtime);

    }


    /**
     * 通过订单号获取商品信息
     */
    private void getOrderInfoByOrderno(){
        HashMap<String,String> map=new HashMap<>();
        map.put("orderno",orderNo);
        Gson gson=new Gson();
        String parmas=gson.toJson(map);
        OkHttpUtils
                .post()
                .url(Config.url+"order/searchOrderDetail.do")
                .addParams("data",parmas)
                .addParams("mobile","true")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        System.out.print("订单详情=="+response);
                        JSONArray jsonArray=null;
                        JSONObject jsonObject=null;
                        ArrayList<HashMap<String,Object>> mapArrayList=new ArrayList<HashMap<String, Object>>();

                        try {
                            jsonArray=new JSONArray(response);
                            for (int i=0;i<jsonArray.length();i++){
                                HashMap<String,Object> map1=new HashMap<String, Object>();
                                jsonObject=jsonArray.getJSONObject(i);
                                map1.put("price",jsonObject.getString("saleprice"));
                                map1.put("proname",jsonObject.getString("proname"));
                                map1.put("count",jsonObject.getString("count"));
                                map1.put("picname",Config.url+"tmp/"+jsonObject.getString("picname"));
                                mapArrayList.add(map1);

                                orderno.setText(jsonObject.getString("orderno"));
                                name.setText(jsonObject.getString("custname"));
                                phone.setText(jsonObject.getString("custphone"));
                                address.setText(jsonObject.getString("receiveraddr"));
                                if (jsonObject.getString("pickupway").equals("0")){
                                    pickupway.setText("配送");
                                }else if (jsonObject.getString("pickupway").equals("1")){
                                    pickupway.setText("自取");
                                }

                                if(jsonObject.getString("paymethod").equals("0")){
                                    payMethod.setText("微信");
                                }else if (jsonObject.getString("paymethod").equals("1")){
                                    payMethod.setText("支付宝");
                                }else if (jsonObject.getString("paymethod").equals("-2")){
                                    payMethod.setText("金币");
                                }else if (jsonObject.getString("paymethod").equals("2")){
                                    payMethod.setText("开联通");
                                }else if (jsonObject.getString("paymethod").equals("6")){
                                    payMethod.setText("联行支付");
                                }

                                psTime.setText(getTime(jsonObject.getString("beginsendtime")));
                                getTime.setText(getTime(jsonObject.getString("endsendtime")));
                                allMoney.setText(jsonObject.getString("ordermoney"));
                                realMoney.setText(jsonObject.getString("ordermoney"));
                                createTime.setText(getTime(jsonObject.getString("createtime")));
                            }
                            PubAdapter myAdapter=new PubAdapter(getContext(),mapArrayList);
                            recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
                            recyclerView.setAdapter(myAdapter);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                });
    }

    private String getTime(String time){
        if(!time.equals("null")){
            Date date=new Date(Long.valueOf(time));
            SimpleDateFormat format;
            format=new SimpleDateFormat("yyyy-MM-dd HH:mm");
            return format.format(date);
        }else {
            return "";
        }
    }
}
